package com.lsl.bhllserver.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsl.bhllserver.dao.func.*;
import com.lsl.bhllserver.dao.template.DAOTemplate;
import com.lsl.bhllserver.entity.Order;
import com.lsl.bhllserver.mapper.OrderMapper;

@Repository
public class OrderDAO implements DAOTemplate<Order> {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public boolean insert(Object object) {
        IInsert insertor = (ordertemp) -> {
            return orderMapper.insert((Order)ordertemp) > 0;
        };
        try{
            return insertor.insert(object);
        } catch (Exception e) {
            System.out.println("[OrderDAO] +++++ insert 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Integer id, Object object) {
        IUpdate updater = (idtemp, ordertemp) -> {
            Order ordertemp2 = (Order)ordertemp;
            ordertemp2.setId(idtemp);
            return orderMapper.updateById(ordertemp2) > 0;
        };
        try{
            return updater.update(id, object);
        } catch (Exception e) {
            System.out.println("[OrderDAO] +++++ update 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        IDelete deleter = (idtemp) -> {
            return orderMapper.deleteById(idtemp) > 0;
        };
        try{
            return deleter.delete(id);
        } catch (Exception e) {
            System.out.println("[OrderDAO] +++++ delete 的 Lambda 方法出错：");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Order> getAll() {
        return orderMapper.selectList(null);
    }

    @Override
    public Order findByID(String id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<Order> findByKeywords(String[] keywords) {
        IQuery<Order> queryor = (kw) -> {
            QueryWrapper<Order> qw = new QueryWrapper<>();
            qw.like("id", kw[0]);
            qw.like("cus_id", kw[1]);
            qw.like("prod_id", kw[2]);
            qw.like("order_postman", kw[3]);
            qw.like("order_status", kw[4]);
            return orderMapper.selectList(qw);
        };
        try {
            return queryor.query(keywords);
        } catch (Exception e) {
            System.out.println(" OrderDAO +++++ 的 findByKeywords Lambda 方法出错");
            e.printStackTrace();
            return null;
        }
    }

    public List<Order> findByCusID(String cusid) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("cus_id", cusid);
        return orderMapper.selectList(qw);
    }

    public List<Order> findByProdID(String prodid) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.eq("prod_id", prodid);
        return orderMapper.selectList(qw);
    }

    // public boolean changeStatus(String id, Integer code) {
    //     Order temp = orderMapper.selectById(id);
    //     temp.setOrderStatus(code);
    //     return orderMapper.updateById(temp) > 0;
    // }

}
